home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / shells / kiss-0.11 / kiss-0 / kiss / src / copydirtodir.c < prev    next >
C/C++ Source or Header  |  1995-03-23  |  2KB  |  79 lines

  1. #include "kiss.h"
  2.  
  3. int copydirtodir (char *srcdir, char *destdir, CpFlags fl, int makeit)
  4. {
  5.     register int
  6.     res = 0;
  7.     glob_t
  8.     globvec;
  9.     register int
  10.     i;
  11.     register char
  12.     *entry,
  13.     *curdir = getcwd (NULL, 0),
  14.     *ddir;
  15.     struct stat
  16.     statbuf;
  17.     char
  18.     fulldest [FILENAMELEN];
  19.  
  20.     /* expand destdir to full path */
  21.     if (makeit)
  22.     {
  23.     strcpy (fulldest, destdir);
  24.     if (fulldest [strlen (fulldest) - 1] != '/')
  25.         strcat (fulldest, "/");
  26.     strcat (fulldest, srcdir);
  27.  
  28.     if (stat (fulldest, &statbuf))
  29.     {
  30.         if (mkdir (fulldest, CREATEFLAGS))
  31.         return (warning ("failure creating dir \"%s\"", fulldest));
  32.     }
  33.     if (chdir (fulldest))
  34.         return (warning ("cannot access dest dir \"%s\"", fulldest));
  35.     }
  36.     else if (chdir (destdir))
  37.     return (warning ("cannot access dest dir \"%s\"", destdir));
  38.  
  39.     ddir = getcwd (NULL, 0);
  40.  
  41.     chdir (curdir);
  42.     if (chdir (srcdir))
  43.     return (warning ("cannot access source dir \"%s\"", srcdir));
  44.     
  45.     if (! glob (".*", 0, NULL, &globvec))
  46.     {
  47.     for (i = 0; i < globvec.gl_pathc; i++)
  48.     {
  49.         entry = globvec.gl_pathv [i];
  50.         if (strcmp (entry, ".") && strcmp (entry, ".."))
  51.         {
  52.         if (! stat (entry, &statbuf) && S_ISDIR (statbuf.st_mode))
  53.             res += copydirtodir (entry, ddir, fl, 1);
  54.         else
  55.             res += copyfiletodir (entry, ddir, fl);
  56.         }
  57.     }
  58.     }
  59.     globfree (&globvec);
  60.  
  61.     if (! glob ("*", 0, NULL, &globvec))
  62.     {
  63.     for (i = 0; i < globvec.gl_pathc; i++)
  64.     {
  65.         entry = globvec.gl_pathv [i];
  66.         if (! stat (entry, &statbuf) && S_ISDIR (statbuf.st_mode))
  67.         res += copydirtodir (entry, ddir, fl, 1);
  68.         else
  69.         res += copyfiletodir (entry, ddir, fl);
  70.     }
  71.     }
  72.     globfree (&globvec);
  73.  
  74.     chdir (curdir);
  75.     free (curdir);
  76.  
  77.     return (res);
  78. }
  79.